<?php
namespace PsiFramework\Applications\AlloMediaPortal\Classes\DAO;

import("Classes.DAO.AbstractDAO");
import("Applications.AlloMediaPortal.Classes.Model.ResponseId");

use \PsiFramework\Classes\DAO\AbstractDAO;
use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Classes\Util\Database\Filters\QueryFilter;
use \PsiFramework\Classes\Util\Database\Query\SqlColumn;
use \PsiFramework\Classes\Util\Database\Query\SqlFunction;
use \PsiFramework\Classes\Util\Database\Query\SqlOperation;
use \PsiFramework\Classes\Util\Database\Query\SqlTable;
use \PsiFramework\Classes\Util\Database\Query\SqlValue;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;
use \PsiFramework\Applications\AlloMediaPortal\Classes\Model\ResponseId;

/**
 * AlloMedia
 * Portail des professionnels
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The ResponseIdDAO is the Data Access Object for the class ResponseId
 *
 * @package DAO
 */
class ResponseIdDAO extends AbstractDAO
{

    protected static $_singleton = null;

    public function retrieveByEntryAndTheme(/*Entry*/ $entry, /*Form*/ $theme)
    {

        if (is_null($entry) || is_null($theme)) {
        	return null ;
        }

        $colEntry = new SqlColumn("entry_id");
        $sqlEntryValue = new SqlValue($entry->getId());

        $equalEntry = new SqlOperation(SqlOperation::$equal);
        $equalEntry->addParameter($colEntry);
        $equalEntry->addParameter($sqlEntryValue);

        $colTheme = new SqlColumn("form_id");
        $sqlThemeValue = new SqlValue($theme->getId());

        $equalTheme = new SqlOperation(SqlOperation::$equal);
        $equalTheme->addParameter($colTheme);
        $equalTheme->addParameter($sqlThemeValue);

        $and = new SqlOperation(SqlOperation::$and);
        $and->addParameter($equalEntry);
        $and->addParameter($equalTheme);

        $responseIds = $this->retrieveAll(new QueryFilter($and));

        if (!is_null($responseIds) && count($responseIds)>0) {
            return $responseIds[0] ;
        } else {
        	return null ;
        }

    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return ResponseIdDAO    instance
     */
    public static function getInstance(ObjectParameters $objectParameters)
    {

        if (self::$_singleton==null) {
            self::$_singleton = new ResponseIdDAO($objectParameters);
        }

        return self::$_singleton;
    }

}
